<?php

class AuthModel extends Model {
	public $tableName = "user";
	
	protected $belongs_to = array('bm_role');
	protected $has_many = array('hm_wall' , 'hm_photo', 'hm_notification');

    public static $ACCESS_EVERY = 3;
    public static $ACCESS_FOLLOWER = 2;
    public static $ACCESS_PRIVATE = 1;
	
	public function __construct($query = null , $username = null , $password = null) {
		parent::__construct($query);
		if ($username != null && $password != null) {
			$this->username = $username;
			$this->password = md5($password);
		}
	}

        public function isWallAllowed($user) {
            if ($this->profile_rule == self::$ACCESS_EVERY && $user != null ||
                $this->profile_rule == self::$ACCESS_FOLLOWER && $user->isFollowing($this) ||
                $this->id == $user->id
                )
                return true;
            else
                return false;
        }

        public function getProfileLink() {
            return '<a href="'.$GLOBALS['controller']->createUrl('profile/view/' . $this->id).'">'.$this->fullname.'</a>';
        }

        public function getInbox() {
            return Model::loadRowsFromQuery("SELECT * FROM message WHERE recipient_id = ".$this->id.";", 'MessageModel');
        }

        public function getOutbox() {
            return Model::loadRowsFromQuery("SELECT * FROM message WHERE sender_id = ".$this->id.";", 'MessageModel');
        }

        public function getFollowers() {
            $relations = Model::getRowsFromQuery("SELECT * FROM `following` WHERE `followed_id` = ".$this->id.";");
            $retval = array();
            foreach ($relations as $r) {
                $model = Model::factory('auth' , $r['follower_id']);
                $retval[] = $model;
            }
            return $retval;
        }

        public function getFollowed() {
            $relations = Model::getRowsFromQuery("SELECT * FROM `following` WHERE `follower_id` = ".$this->id.";");
            $retval = array();
            foreach ($relations as $r) {
                $model = Model::factory('auth' , $r['followed_id']);
                $retval[] = $model;
            }
            return $retval;
        }

        public function isFollowing($user) {
            return in_array($this , $user->getFollowers());
        }

        public function isFollowed($user) {
            return in_array($this , $user->getFollowed());
        }

        public function follow($user_id) {
            $u_id = $this->id;
            Model::query("INSERT INTO `following`(`follower_id` , `followed_id`) VALUES($u_id , $user_id);");
        }

        public function unfollow($user_id) {
            $u_id = $this->id;
            Model::query("DELETE FROM `following` WHERE follower_id = $u_id AND followed_id = $user_id;");
        }
	
	public function getProfilePicture() {
		$pp = new PhotoModel("SELECT * FROM photo WHERE id = ".$this->photo_id.";");
                if ($pp->loaded())
                    return $pp;
                else
                    return null;
	}
	
	public function setPassword($password) {
		$this->password = md5($password);
	}
	
	public function logout() {
		unset($_SESSION['auth']);
	}
	
	public function login($u , $p) {
		//TODO : check database
		$this->username = $u;
		$this->setPassword($p);
		
		$selectedUser = new AuthModel("SELECT * FROM user WHERE `username` = '" . $this->username . "';");
		
		if ($selectedUser->password == $this->password && $selectedUser->enabled) {
			$_SESSION['auth'] = $selectedUser;
			return true;
		}
		else
			return false;
	}
	
	public function getRole() {
		//TODO :  check database
	}
}

?>